'use strict';

function ajaxPost(url, json, cb) {
  $.ajax({
    url: url,
    type: "POST",
    headers: {"Content-Type": "application/json;charset=UTF-8"},
    data: JSON.stringify(json),
    success: function (res) {
      cb && cb(null, res);
    },
    error: function (xhr) {
      cb && cb(xhr.responseText);
    }
  });
}
function ajaxGet(url, cb) {
  $.ajax({
    url: url,
    type: "GET",
    success: function (res) {
      cb && cb(null, res);
    },
    error: function (xhr) {
      cb && cb(xhr.responseText);
    }
  });
}

function register(name, pwd, userType, cb) {
  var url = '/api/v1/auth/register';
  var json = {name: name, pwd: pwd, userType: userType};
  ajaxPost(url, json, cb);
}

function login(name, pwd, userType, cb) {
  var url = '/api/v1/auth/login';
  var json = {name: name, pwd: pwd, userType: userType};
  ajaxPost(url, json, cb);
}

function addLoc(cityName, zoneName, cb) {
  var url = '/api/v1/location';
  var json = {cityName: cityName, zoneName: zoneName};
  ajaxPost(url, json, cb);
}

function allLoc(cb) {
  var url = '/api/v1/location';
  ajaxGet(url, cb);
}

function addCate(name, cb) {
  var url = '/api/v1/category';
  var json = {name: name};
  ajaxPost(url, json, cb);
}

function allCate(cb) {
  var url = '/api/v1/category';
  ajaxGet(url, cb);
}

function addWare(name, categoryId, quantity, price, cb) {
  var url = '/api/v1/vendor/ware';
  var json = {name: name, categoryId: categoryId, quantity: quantity, price: price};
  ajaxPost(url, json, cb);
}

function allWare(cb) {
  var url = '/api/v1/vendor/ware';
  ajaxGet(url, cb);
}

function addShop(name, locationId, cb) {
  var url = '/api/v1/vendor/shop';
  var json = {name: name, locationId: locationId};
  ajaxPost(url, json, cb);
}

function allShop(cb) {
  var url = '/api/v1/vendor/shop';
  ajaxGet(url, cb);
}